%% Impact of an uncertainty shock on switching probabilities

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: IM
% 
% Purpose: Simulates an uncertainty shock in a population of firms
% and compute switching probabilities, before and after the shock
% 
% Input: 
%   1) results from simulation.m
% 
% Output: Table 2
%
% Last Updated: 10/10/2023
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


rng(2);
% Specify 
% number of periods (1000 with a shock after 500 periods)
% number of firms (2000)
% number of income path (500)
n_periods=1000;
n_firms=2000;
n_sim=500;


% Run a loop over the three values used to characterize the degree of
% stickiness
for s=1:3
p_reservation_highuncertainty=squeeze(p_reservation_highuncertainty_all(:,s,:));
p_reservation_lowuncertainty=squeeze(p_reservation_lowuncertainty_all(:,s,:));
H_p_reservation_highuncertainty=squeeze(H_p_reservation_highuncertainty_all(:,s,:));
H_p_reservation_lowuncertainty=squeeze(H_p_reservation_lowuncertainty_all(:,s,:));

parfor  d=1:n_sim

p_index=ones(n_firms,n_periods);
p_value=ones(n_firms,n_periods);
I_index=ones(n_periods,1);
duration=zeros(n_firms,n_periods);
switch_price=zeros(n_firms,n_periods);
death=zeros(n_firms,n_periods);
p_index_vp=zeros(n_firms,n_periods);
for i=1:n_periods
if i==1
% We choose initial Income index!
I_index(1)=20 ;
elseif i<n_periods/2
I_index(i)=find(G_I_cond_I0_low(I_index(i-1),:)>rand(),1);
elseif i==n_periods/2
%change of uncertainty
I_index(i)=find(G_I_cond_I0_high(I_index(i-1),:)>rand(),1);
elseif i>n_periods/2
    I_index(i)=I_index(i-1);
end

for j=1:n_firms
p_index(j,i)=find(cdf_p>rand(),1);
end

for j=1:n_firms
    if  i==1    
        p_value(j,i)=  p_default(p_index(j,i));
        switch_price(j,i)=0;
        death(j,i)=0;
    else
   %Firm dies with probability delta
       if delta>rand()
            death(j,i)=1;
            p_value(j,i)=p_default(1,p_index(j,i));
            switch_price(j,i)=0;
       else 
           % firm meets a new partner with probability lambda
       if lambda>rand()
            death(j,i)=0;
            p_index_vp(j,i)=find(p_default==p_value(j,i-1));

                %Firm switches if the newly matched firms offer a price
                %below its reservation price (difference under low and high
                %uncertainty
             if p_default(p_index(j,i))<=p_reservation_lowuncertainty(p_index_vp(j,i),I_index(i)) & i<n_periods/2-1
                p_value(j,i)=p_default(1,p_index(j,i));
                duration(j,i)=0;
                switch_price(j,i)=1;
             elseif p_default(p_index(j,i))<=p_reservation_highuncertainty(p_index_vp(j,i),I_index(i)) & i>n_periods/2-2
                p_value(j,i)=p_default(1,p_index(j,i));
                duration(j,i)=0;
                switch_price(j,i)=1;
              else 
                p_value(j,i)=p_value(j,i-1);
                duration(j,i)=duration(j,i-1)+1; 
                switch_price(j,i)=0;
           end
           else
                death(j,i)=0;  
                p_value(j,i)=p_value(j,i-1);
                duration(j,i)=duration(j,i-1)+1;
                switch_price(j,i)=0;
           end
       end
    end
end

end
% Compute the cumulative distribution function of Pvalue

mean_switch_price(d,:)=mean(switch_price);

end

mean_switch_price_all_d(s,:,:)=mean_switch_price;
end

Table2=zeros(3,3);
Table2(1,1)=mean(mean(squeeze(mean_switch_price_all_d(1,:,n_periods/2-5:n_periods/2-2))));
Table2(2,1)=mean(mean(squeeze(mean_switch_price_all_d(1,:,n_periods/2-1))));
Table2(3,1)=(Table2(2,1)-Table2(1,1))/Table2(1,1)*100;
Table2(1,2)=mean(mean(squeeze(mean_switch_price_all_d(2,:,n_periods/2-5:n_periods/2-2))));
Table2(2,2)=mean(mean(squeeze(mean_switch_price_all_d(2,:,n_periods/2-1))));
Table2(3,2)=(Table2(2,2)-Table2(1,2))/Table2(1,2)*100;
Table2(1,3)=mean(mean(squeeze(mean_switch_price_all_d(3,:,n_periods/2-5:n_periods/2-2))));
Table2(2,3)=mean(mean(squeeze(mean_switch_price_all_d(3,:,n_periods/2-1))));
Table2(3,3)=(Table2(2,3)-Table2(1,3))/Table2(1,3)*100;
save Results/Table2.mat Table2;

